package org.ziwei.controller;

import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.ziwei.paipan.MingPan;
import org.ziwei.paipan.MingPanFactory;
import org.ziwei.paipan.var.Sex;
import org.ziwei.rule.api.RuleHandlerChain;

@Controller
public class MingPanController {

	@Autowired
	MingPanFactory factory;
	
	@Autowired
	RuleHandlerChain ruleHandlerChain;
	
	@RequestMapping(value = "/")
	public String index(HttpServletRequest request){
		return "mingpan";
	}
	
	@RequestMapping(value = "/mingpan")
	public String mingpan(HttpServletRequest request){
		return "mingpan";
	}	
	
	@ResponseBody
	@RequestMapping("/test")
	Object test(String birthday, String sex,Integer curYear) throws Exception {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		return factory.createMingPan(df.parse(birthday), sex.equalsIgnoreCase("M") ? Sex.M : Sex.F, curYear);
	}
	
	@ResponseBody
	@RequestMapping("/getMingPan")
	Object getMingPan(String birthday, String sex,Integer curYear) throws Exception {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		return factory.createMingPan(df.parse(birthday), sex.equalsIgnoreCase("M") ? Sex.M : Sex.F, curYear);
	}

	@ResponseBody
	@RequestMapping("/jiePan")
	Object jiePan(String birthday, String sex,Integer curYear) throws Exception {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		MingPan pan = factory.createMingPan(df.parse(birthday), sex.equalsIgnoreCase("M") ? Sex.M : Sex.F, curYear);
		return ruleHandlerChain.fireAllRules(pan);
	}
}
